import request from '@/utils/request';
import { downloadBlob } from '@/utils/utils';
import { TableListParams } from '@/pages/TableList/data';
/**
 * 统一化导出接口
 * params 点击导出时的查询条件 必 Object
 * url 导出时请求的路径   必  String
 * fileName参数 导出的文件名 非必 String
 */

export const exportFile = (params: any, url: string, fileName: string, totalUrl?: string) => {
  if (!totalUrl) {
    return request(url, {
      method: 'get',
      responseType: 'blob',
      getResponse: true,
      params,
    }).then((res) => {
      if (!res) {
        return;
      }
      downloadBlob(res, fileName);
    });
  }
  return request(totalUrl, {
    method: 'get',
    params,
  }).then((res) => {
    if (res.code === '2000') {
      request(url, {
        method: 'get',
        responseType: 'blob',
        params,
      }).then((response: any) => {
        if (!response) {
          return;
        }
        downloadBlob(res, fileName);
      });
    }
  });
};

// 获取列表数据
export async function queryList(apiUrl: string, params?: TableListParams) {
  return request(apiUrl, {
    params,
  });
}
